﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerTrinhDoDao : ITrinhDoDao
    {
        public SqlServerTrinhDoDao()
        {
            SQLUtility.TableName = "TrinhDo";
        }

        //Trả về đối tượng báo ăn
        private TrinhDo MakeTrinhDo(DataRow row)
        {
            Guid _id = Guid.Parse(row["TrinhDoID"].ToString());
            string _tenTrinhDo = row["TenTrinhDo"].ToString();
            string _soTrinhDo = row["SoTrinhDo"].ToString();
            int _schoolID = int.Parse(row["SchoolID"].ToString());
            return new TrinhDo(_id, _tenTrinhDo, _soTrinhDo, _schoolID);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<TrinhDo> MakeTrinhDos(DataTable dt)
        {
            IList<TrinhDo> list = new List<TrinhDo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeTrinhDo(row));
            }
            return list;
        }
        private DbParameter[] Take(TrinhDo TrinhDo)
        {
            DbParameter[] parameters = new DbParameter[4];
            parameters[0] = new SqlParameter("@TrinhDoID", TrinhDo.Id);
            parameters[1] = new SqlParameter("@TenTrinhDo", TrinhDo.TenTrinhDo);
            parameters[2] = new SqlParameter("@SoTrinhDo", TrinhDo.SoTrinhDo);
            parameters[3] = new SqlParameter("@SchoolID", TrinhDo.SchoolID);
            return parameters;
        }

        public IList<Business_Objects.TrinhDo> GetTrinhDo()
        {
            SQLUtility.TableName = "TrinhDo";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeTrinhDos(dt);
        }

        public Business_Objects.TrinhDo GetTrinhDoByID(string TrinhDoID)
        {
            SQLUtility.TableName = "TrinhDo";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@TrinhDoID", TrinhDoID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeTrinhDo(row);
            }
        }

        public void InsertTrinhDo(Business_Objects.TrinhDo TrinhDo)
        {
            SQLUtility.TableName = "TrinhDo";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(TrinhDo), true);
        }

        public int UpdateTrinhDo(Business_Objects.TrinhDo TrinhDo)
        {
            SQLUtility.TableName = "TrinhDo";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(TrinhDo)); }
            catch { return -1; }
        }

        public int DeleteTrinhDo(Business_Objects.TrinhDo TrinhDo)
        {
            SQLUtility.TableName = "TrinhDo";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(TrinhDo)); }
            catch { return -1; }
        }


        public List<TrinhDo> GetTrinhDoBySchoolId(int schoolId)
        {
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@SchoolID", schoolId);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectTrinhDosByAndSchoolID", parameters);
            return MakeTrinhDos(dt).ToList(); 
        }
    }
}
